/*
 * Created on Jan 5, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.common.reporting.pdf;



import java.awt.Color;
import java.awt.Polygon;
import java.awt.BasicStroke;
import java.awt.FontMetrics;
import org.apache.log4j.Logger;
import java.awt.Graphics2D;
import java.awt.Font;




/**
 * @author mmoore
 */
public class LogoWriter {
	private transient static Logger log = Logger.getLogger(LogoWriter.class);
	//private float height = 0;
	private float widthToHeightRatio = (40 / 10);	
	
	public LogoWriter(Graphics2D g2) {
		super();
	}

	public int getWidth(float logoHeight, Graphics2D g2) {
		return(render(0, 0, logoHeight, g2, true));
	}
	
	public void drawLogo(float x, float y0, float logoHeight, Graphics2D g2) {
		render(x, y0, logoHeight, g2, false);
	}


	public void drawDiamond(float x, float y, float width, float height, Graphics2D g2, Color color) {
		// Bottom blue diamond
		g2.setPaint(color);
		g2.setStroke(new BasicStroke(0f)); 
		Polygon polygon = createDiamond((int)(x), (int)(y), (int)width, (int)height);
		g2.fillPolygon(polygon);
	}

	
	protected int render(float x, float y0, float logoHeight, Graphics2D g2, boolean widthOnly) {
		int totalWidth = 0;
		float height = logoHeight / 5;
		float width = height * widthToHeightRatio;
		float spacingX = (int)((logoHeight * 8) / 100);
		if (spacingX < 1) 
			spacingX = 1;
		float spacingY = (int)((spacingX * height) / width);
		if (spacingY < 1) 
			spacingY = 1;
		float y = y0 + ((32 * logoHeight) / 50);

		log.debug("drawLogo() - spacingX = " + spacingX);
		log.debug("drawLogo() - spacingY = " + spacingY);
		int graphicsHeight = 1000;
		int graphicsWidth = 1000;
		


		if (widthOnly == false) {
			g2.setPaint(new Color(80, 130, 230));
			g2.setStroke(new BasicStroke(0f)); 

			// Blue diamonds
			float translateX = 0;
			float translateY = 0;
			log.debug("Printing top blue diamond.  translateX = " + translateX + "  translateY = " + translateY);
			Polygon polygon = createDiamond((int)(translateX + x), (int)(translateY + y), (int)width, (int)height);
			g2.fillPolygon(polygon);
	
			// Right blue diamond
			translateX = spacingX + (width / 2);
			translateY = spacingY + (height / 2);
			log.debug("Printing right blue diamond.  translateX = " + translateX + "  translateY = " + translateY);
			polygon = createDiamond((int)(translateX + x), (int)(translateY + y), (int)width, (int)height);
			g2.fillPolygon(polygon);
			
			// Bottom blue diamond
			translateX = 0;
			translateY = (2 * spacingY) + height;
			log.debug("Printing bottom blue diamond.  translateX = " + translateX + "  translateY = " + translateY);
			polygon = createDiamond((int)(translateX + x), (int)(translateY + y), (int)width, (int)height);
			g2.fillPolygon(polygon);
	
			// Left green diamond
			g2.setPaint(new Color(80, 220, 80));
			translateX = spacingX + (width / 2);
			translateY = - (spacingY + (height / 2));
			log.debug("Printing left green diamond.  translateX = " + translateX + "  translateY = " + translateY);
			polygon = createDiamond((int)(translateX + x), (int)(translateY + y), (int)width, (int)height);
			g2.fillPolygon(polygon);
	
			// Bottom green diamond
			translateX = (2 * spacingX) + (width);
			translateY = 0;
			log.debug("Printing bottom green diamond.  translateX = " + translateX + "  translateY = " + translateY);
			polygon = createDiamond((int)(translateX + x), (int)(translateY + y), (int)width, (int)height);
			g2.fillPolygon(polygon);
			
			// Top green diamond
			translateX = (2 * spacingX) + (width);
			translateY = - ((2 * spacingY) + height);
			log.debug("Printing top green diamond.  translateX = " + translateX + "  translateY = " + translateY);
			polygon = createDiamond((int)(translateX + x), (int)(translateY + y), (int)width, (int)height);
			g2.fillPolygon(polygon);
		}

		int nameStartX = (int)(x + ((width * 5) / 2));
		g2.setPaint(new Color(80, 90, 30));
		Font font = new Font("Helvetica", Font.PLAIN, (int)((height * 30)/ 10));
		g2.setFont(font);
		if (widthOnly == false) {
			g2.drawString("VECTRICS", (int)nameStartX, (int)(y + ((30 * logoHeight) / 100)));
		}

		FontMetrics fontMetrics = g2.getFontMetrics(font);
		int nameWidth = fontMetrics.stringWidth("VECTRICS");

		font = new Font("Helvetica", Font.PLAIN, (int)((height * 50) / 50));
		g2.setFont(font);
		int tmLeft = nameStartX + nameWidth + (int)(logoHeight / 10);
		if (widthOnly == false) {
			g2.drawString("TM", tmLeft, y - ((5 * logoHeight / 50)));
			g2.dispose();
		}
		FontMetrics tmFontMetrics = g2.getFontMetrics(font);
		int tmWidth = tmFontMetrics.stringWidth("TM");
		totalWidth =  tmLeft + tmWidth + 1;
		
		return(totalWidth); 
	}
	
	private Polygon createDiamond(int x, int y, int width, int height) {
		Polygon polygon = new Polygon();
		polygon.addPoint(x, y);
		polygon.addPoint(x + (width / 2), y + (height/2));
		polygon.addPoint(x + width,  y);
		polygon.addPoint(x + (width / 2), y - (height/2));
		polygon.addPoint(x,  y);
		return(polygon);
	}	
	

}
